package com.gljx.web.controller.JournalController;

import com.gljx.web.Entity.*;
import com.gljx.web.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/")
public class FuelStationJournalController {

	@Autowired
	private DepartmentService departmentService;
	
	@Autowired
	private FillingWareRegEntryService fillingWareRegEntryService;

	@Autowired
	private OilspecService oilspecService;
	
	@Autowired
	private OtherCarAddOilEntryService otherCarAddOilEntryService;
	
	@Autowired
	private CarRefuelEntryService carRefuelEntryService;
	
	
	/**
	 * 进入选择加油站进销存报表条件
	 * @return
	 * @createTime 2017-12-15 10:46 am
	 * @author zyq.
	 */
	@RequestMapping(value="/chooseFuelStationJournal")
	public String chooseFuelStationJournal(HttpServletRequest request){
		HttpSession session=request.getSession();
		User user=(User) session.getAttribute("user");                                   //获取已经登录用户数据
		Department department=departmentService.queryAdminByFid(user.getFcompanyid());   //获取登录用户组织ID
		Calendar cale = Calendar.getInstance();// 得到一个Calendar的实例
		  		 cale.add(Calendar.MONTH, 0);
		  		 cale.set(Calendar.DAY_OF_MONTH, 1);
		Date resultDate = cale.getTime(); // 结果
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String beginTime = sdf.format(resultDate);
		String endTime = sdf.format(new Date());
		List<Oilspec> oList = oilspecService.queryOilsperByFcontrolunitid(user.getFcompanyid());
		//把数据传到页面
		request.setAttribute("oLists", oList);
		request.setAttribute("company", department.getFnameL2());
		request.setAttribute("beginTime", beginTime);
		request.setAttribute("endTime", endTime);
		return "backHome/journaling/stationJournaling/fuelStationJournal";	
	}
	
	
	/**
	 * 根据名称查询
	 * @param request
	 * @param name
	 * @return
	 */
	@RequestMapping(value="/selFuelStationJournalByName")
	@ResponseBody
	public List<Oilspec> selFuelCardJournalByName(HttpServletRequest request,String name){
		List<Oilspec> fillingCards = oilspecService.queryOilsperByName(name);
		return fillingCards;
	}
	
	/**
	 * 根据fid查询加油卡记录
	 * @param request
	 * @param fid
	 * @return
	 */
	@RequestMapping(value="/selFuelStationJournalById")
	@ResponseBody
	public Oilspec selFuelStationJournalById(HttpServletRequest request,String fid){
		Oilspec oilspec = oilspecService.queryByPrimaryKey(fid);
		return oilspec;
	}
	
	/**
	 * 查询报表所需数据
	 * @param request
	 * @param beginTime
	 * @param endTime
	 * @param fname
	 * @param fid
	 * @param companyId
	 * @return
	 */
	@RequestMapping(value="/selFuelStationJournal")
	public String selFuelStationJournal(HttpServletRequest request,String beginTime,String endTime,String fname,String fid) {
		HttpSession session=request.getSession();
		User user=(User) session.getAttribute("user");
		Department department=departmentService.queryAdminByFid(user.getFcompanyid());   //获取登录用户组织ID
		Map<String , Object> map = new HashMap<>();
							 map.put("fid",fid);
							 map.put("beginTime", beginTime);
							 map.put("endTime", endTime);
							 map.put("companyId", user.getFcompanyid());
		Double fillingBeginBalance = fillingWareRegEntryService.selFillingWareBeginBalance(map); 	//期初加油卡
		Double carBeginBalance = carRefuelEntryService.selCarRefuelBeginBalance(map);
		Double carBeginBalances = carRefuelEntryService.selCarRefuelBeginBalances(map);
		Double otherBeginBalance = otherCarAddOilEntryService.selOtherCarAddOil(map);
		Double otherBeginBalances = otherCarAddOilEntryService.selOtherCarAddOils(map);
		Double fillingBeginBalances = fillingWareRegEntryService.selFillingWareBeginBalances(map);
		Double beginBalance = null ;
		if(fillingBeginBalance == null || otherBeginBalance == null || carBeginBalance == null){
			beginBalance = null ;
		}else {
			beginBalance = fillingBeginBalance-otherBeginBalance-carBeginBalance;
			BigDecimal   beg = new   BigDecimal(beginBalance);  
			beginBalance = beg.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue(); 	   
		}		
		List<FillingWareRegEntry> fillingWareRegEntries = fillingWareRegEntryService.selFillingWareRegEntry(map);
		List<OtherCarAddOilEntry> otherCarAddOilEntries = otherCarAddOilEntryService.selOtherCarAddOilEntry(map);
		List<CarRefuelEntry> carRefuelEntries = carRefuelEntryService.selCarRefuelEntry(map);
		Double endBalance = null;
		if(beginBalance == null)
			beginBalance = Double.valueOf(0);
		if(fillingBeginBalances != null & otherBeginBalances != null & carBeginBalances != null){
		      endBalance = beginBalance+fillingBeginBalances-otherBeginBalances-carBeginBalances;
		BigDecimal   end = new   BigDecimal(endBalance);  
			   endBalance = end.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue(); 	
		}else if(fillingBeginBalances != null & otherBeginBalances != null){
		    endBalance = beginBalance+fillingBeginBalances-otherBeginBalances;
			BigDecimal   end = new   BigDecimal(endBalance);  
			endBalance = end.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue(); 	
		}else if(fillingBeginBalances != null & carBeginBalances != null){
		    endBalance = beginBalance+fillingBeginBalances-carBeginBalances;
			BigDecimal   end = new   BigDecimal(endBalance);  
			endBalance = end.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue(); 	
		}else if(fillingBeginBalances == null & otherBeginBalances == null & carBeginBalances == null & beginBalance == null ){
		    endBalance = null;	
		}else if(fillingBeginBalances != null & beginBalance != null){
			endBalance = beginBalance+fillingBeginBalances;
			BigDecimal   end = new   BigDecimal(endBalance);  
			endBalance = end.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue(); 	
		}else {
			endBalance = beginBalance;
			BigDecimal   end = new   BigDecimal(endBalance);  
			endBalance = end.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue(); 	
		}
		List<Oilspec> oList = oilspecService.queryOilsperByFcontrolunitid(user.getFcompanyid());
		//把数据传到页面
		request.setAttribute("oLists", oList);
		request.setAttribute("endBalance", endBalance);
		request.setAttribute("fillingWareRegEntries", fillingWareRegEntries);
		request.setAttribute("otherCarAddOilEntries", otherCarAddOilEntries);
		request.setAttribute("carRefuelEntries", carRefuelEntries);
		request.setAttribute("fname", fname);
		request.setAttribute("fid", fid);
		request.setAttribute("company",department.getFnameL2());
		request.setAttribute("beginBalance", beginBalance);
		request.setAttribute("fillingBeginBalances", fillingBeginBalances);
		request.setAttribute("otherBeginBalances", otherBeginBalances);
		request.setAttribute("carBeginBalances", carBeginBalances);
		request.setAttribute("beginTime", beginTime);
		request.setAttribute("endTime", endTime);
		return "backHome/journaling/stationJournaling/fuelStationJournal";	
	}
	
	
	
	
}
